package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.common.base.RespResult;
import com.hy.common.consts.CommonConst;
import com.hy.common.utils.CommonUtils;
import com.hy.entity.dto.RecordDTO;
import com.hy.entity.pojo.HyPropertyRecord;
import com.hy.entity.vo.HyUserVO;
import com.hy.mapper.HyPropertyRecordMapper;
import com.hy.service.HyPropertyRecordService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HyPropertyRecordServiceImpl implements HyPropertyRecordService {

    @Resource
    private HyPropertyRecordMapper recordMapper;
    @Override
    public RespResult showMyChangeApply() {
        HyUserVO principal = (HyUserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        LambdaQueryWrapper<HyPropertyRecord> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(HyPropertyRecord::getBeforeUserId,principal.getUserId()).eq(HyPropertyRecord::getDel,1);
        List<HyPropertyRecord> records = recordMapper.selectList(lambdaQueryWrapper);
        return RespResult.success(records);
    }

    @Override
    public RespResult getOnePropertyRecord(Integer propertyId) {

        LambdaQueryWrapper<HyPropertyRecord> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(HyPropertyRecord::getPropertyId,propertyId);
        List<HyPropertyRecord> records = recordMapper.selectList(lambdaQueryWrapper);
        return RespResult.success(records);
    }

    @Override
    public RespResult showAllPropertyRecord(RecordDTO recordDTO) {
        HyUserVO principal = (HyUserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<String> roles = principal.getRoles();
        RespResult result=null;
        if (CommonUtils.isMember(roles, CommonConst.SYSTEM_ROLE_NAME)){

           result= getResult(recordDTO);
        }else if (CommonUtils.isMember(roles,CommonConst.PROPERTIES_ROLE_NAME)|| CommonUtils.isMember(roles,CommonConst.UNIT_ROLE_NAME)){

            recordDTO.setUnitId(principal.getUnitId());
            result=getResult(recordDTO);
        }

        return result;
    }

    public  RespResult getResult(RecordDTO recordDTO){
        Page<HyPropertyRecord>  page=new Page<>(recordDTO.getCurrentPage(),recordDTO.getPageSize());
        LambdaQueryWrapper<HyPropertyRecord> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(recordDTO.getRecordState()),HyPropertyRecord::getRecordState,recordDTO.getRecordState());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(recordDTO.getAfterUserName()),HyPropertyRecord::getAfterUsername,recordDTO.getAfterUserName());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(recordDTO.getBeforeUserName()),HyPropertyRecord::getBeforeUsername,recordDTO.getBeforeUserName());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(recordDTO.getPropertyName()),HyPropertyRecord::getPropertyName,recordDTO.getPropertyName());
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(recordDTO.getUnitId()),HyPropertyRecord::getUnitId,recordDTO.getUnitId());
        Page<HyPropertyRecord> recordPage = recordMapper.selectPage(page, lambdaQueryWrapper);
        return RespResult.success(recordPage.getRecords()).setCount(Long.valueOf(recordPage.getTotal()).intValue());
    };

    @Override
    public RespResult showWaitReadApply() {
        HyUserVO principal = getUser();
        LambdaQueryWrapper<HyPropertyRecord> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(HyPropertyRecord::getCheckerId,principal.getUserId());
        lambdaQueryWrapper.eq(HyPropertyRecord::getRecordState, CommonConst.RECORD_CHECKING).eq(HyPropertyRecord::getDel,1);
        List<HyPropertyRecord> records = recordMapper.selectList(lambdaQueryWrapper);
        return RespResult.success(records);
    }

    @Override
    public RespResult propertyHistoryUser(Integer id) {

//        recordMapper.selectList(new LambdaQueryWrapper<HyPropertyRecord>().and(wapper->
//
//
//                ))
        return null;
    }

    public HyUserVO getUser(){
        return (HyUserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
